﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;


namespace ORMAC.Core
{
    /// <summary>
    /// Helpers class for getting values from data set tables
    /// The helper methods support null objects
    /// </summary>
    internal static class DataSetHelper
    {
        /// <summary>
        /// Get a datetime value from table row
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        public  static DateTime? GetDate(this DataRow item, string fieldname)
        {
            if (item == null || string.IsNullOrEmpty(fieldname))
                return null;
            if (!item.Table.Columns.Contains(fieldname)|| item[fieldname] is DBNull)
                return null;
            else
            {
                if (item[fieldname] is DateTime)
                {
                    return  item[fieldname] as DateTime?;
                }
                else
                {
                    string s = item[fieldname] as string;
                    if (s == null)
                        return null;
                    else
                    {
                        DateTime res;
                        bool success=DateTime.TryParse(s,out res);
                        if (success)
                            return res;
                        else
                            return null;
                    }
                }
            }
        }
        /// <summary>
        /// Get string from table row
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        public static string GetString(this DataRow item, string fieldname)
        {
            if (item == null || string.IsNullOrEmpty(fieldname))
                return null;
            if (!item.Table.Columns.Contains(fieldname) || item[fieldname] is DBNull)
                return null;
            else
            {
                string s = item[fieldname] as string;
                if (s == null)
                    return null;
                else
                    return s;
            }
        }
        /// <summary>
        /// Get decimal from table row
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        public static Decimal? GetDecimal(this DataRow item, string fieldname)
        {
            if (item == null || string.IsNullOrEmpty(fieldname))
                return null;
            if (!item.Table.Columns.Contains(fieldname) || item[fieldname] is DBNull)
                return null;
            else
            {
                if (item[fieldname] is Decimal)
                {
                    return item[fieldname] as Decimal?;
                }
                else
                {
                    string s = item[fieldname] as string;
                    if (s == null)
                        return null;
                    else
                        return Decimal.Parse(s);
                }
            }
        }
        /// <summary>
        /// Get integer from table row
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        public static int? GetInt(this DataRow item, string fieldname)
        {
            if (item == null || string.IsNullOrEmpty(fieldname))
                return null;
            if (!item.Table.Columns.Contains(fieldname) || item[fieldname] is DBNull)
                return null;
            else
            {
                return item[fieldname] as int?;
            }
        }
        /// <summary>
        /// Gets the value of a column based on its type
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldname"></param>
        /// <param name="dataType"></param>
        /// <param name="schema"></param>
        /// <returns></returns>
        public static object GetValue(this DataRow item, string fieldname,Type dataType,string schema)
        {
            if (dataType.IsGenericType)
            {
                dataType = dataType.GetGenericArguments()[0];
            }
            if (dataType == typeof(int))
                return GetInt(item, fieldname);
            else if (dataType == typeof(Decimal))
                return GetDecimal(item, fieldname);
            else if (dataType == typeof(DateTime))
                return GetDate(item, fieldname);
            else if(dataType.IsEnum){
                MethodInfo methodInfo = typeof(ReflectionHelper).GetMethod("ToEnum");
                MethodInfo enumMethod = methodInfo.MakeGenericMethod(new Type[]
                { dataType });
                return enumMethod.Invoke(null,new object[]{GetString(item, fieldname),schema});
            }
            else
                return GetString(item, fieldname);
        }
        public static object GetObject(this DataRow item, string fieldname)
        {
            if (item == null || string.IsNullOrEmpty(fieldname))
                return null;
            if (!item.Table.Columns.Contains(fieldname) || item[fieldname] is DBNull)
                return null;
            else
            {
                return item[fieldname] ;
            }
        }

    }
}
